%include {

/** @brief Lemon parser grammar for N-Triples.
 *
 * The `lemon' parser generator executable must be in your PATH:
 * https://sqlite.org/src/doc/trunk/doc/lemon.html
 *
 * To generate the parser, run: `lemon ${FILE}'
 */

#include "graph.h"
}


%token_type { LSUP_Term * }
%token_prefix "T_"

%type triple            { LSUP_Triple * }
%destructor triple      { LSUP_triple_free ($$); }
%type subject           { LSUP_Term * }
%destructor subject     { LSUP_term_free ($$); }
%type predicate         { LSUP_Term * }
%destructor predicate   { LSUP_term_free ($$); }
%type object            { LSUP_Term * }
%destructor object      { LSUP_term_free ($$); }
%default_type           { void * }

%extra_argument         { LSUP_GraphIterator *it }


// Rules.

ntriplesDoc ::= triples EOF.

triples     ::= eol.
triples     ::= triple eol.
triples     ::= triples triple eol.

triple(A)   ::= ws subject(S) ws predicate(P) ws object(O) ws DOT. {

                A = LSUP_triple_new (S, P, O);
                LSUP_graph_add_iter (it, A);
            }

subject     ::= IRIREF.
subject     ::= BNODE.

predicate   ::= IRIREF.

object      ::= IRIREF.
object      ::= BNODE.
object      ::= LITERAL.

eol         ::= EOL.
eol         ::= eol EOL.

ws          ::=.
ws          ::= WS.
